package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.TokenHolder;
import com.itheima.constant.TokenConstant;
import com.itheima.entity.dto.shoppingcart.ShoppingCartDto;
import com.itheima.entity.dto.shoppingcart.ShoppingCartSubDto;
import com.itheima.entity.pojo.ShoppingCart;
import com.itheima.entity.vo.shoppingcart.ShoppingCartAddVo;
import com.itheima.entity.vo.shoppingcart.ShoppingCartListVo;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-12
 */
@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;


    /**
     * 根据用户id获取购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCartListVo> getShoppingCartList() {
        //1.获取当前用户id
        String currentId = TokenHolder.getCurrentId();
        List<ShoppingCartListVo> shoppingCartListVos = new ArrayList<>();

        //2.查询购物车列表
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.SelectByUserId(currentId);

        if(CollUtil.isEmpty(shoppingCarts)){
            return shoppingCartListVos;
        }


        //3.封装成前端对象
        for (ShoppingCart shoppingCart : shoppingCarts) {
            if(Objects.isNull(shoppingCart)){
                continue;
            }

            ShoppingCartListVo shoppingCartListVo = new ShoppingCartListVo();
            BeanUtils.copyProperties(shoppingCart, shoppingCartListVo);

            shoppingCartListVos.add(shoppingCartListVo);

        }

        return shoppingCartListVos;
    }

    /**
     * 添加购物车
     *
     * @return
     */
    @Override
    public ShoppingCartAddVo addShoppingCart(ShoppingCartDto shoppingCartDto ) {
        //1.先查询库中是否存在当前商品
        String dishId = shoppingCartDto.getDishId();
        String userId = TokenHolder.getCurrentId();
        ShoppingCart shoppingCart = shoppingCartMapper.countByDishId(dishId,userId);

        String id=null;
        //2.没有则添加一个商品
        if(Objects.isNull(shoppingCart)){
            SimpleDateFormat spl = new SimpleDateFormat("ss");

            shoppingCart= new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDto, shoppingCart);
            long time1 =System.currentTimeMillis()/1000;
            System.out.println("添加一个商品1:"+time1);


          id=  IdUtil.getSnowflake().nextIdStr();

            long time2 =System.currentTimeMillis()/1000;
            System.out.println("添加一个商品2:"+(time2-time1));

            shoppingCart.setUserId(userId);
            shoppingCart.setId(id);
            long time3 =System.currentTimeMillis()/1000;
            System.out.println("添加一个商品3:"+(time3-time2));
            shoppingCart.setCreateTime(new Date());

            long time4 =System.currentTimeMillis()/1000;
            System.out.println("添加一个商品4:"+(time4-time3));

            shoppingCart.setUpdateTime(new Date());
            long time5 =System.currentTimeMillis()/1000;
            System.out.println("添加一个商品5:"+(time5-time4));

            shoppingCartMapper.insert(shoppingCart);


        }else {
        //3.有则添加数量
            id=shoppingCart.getId();
            BeanUtils.copyProperties(shoppingCartDto, shoppingCart);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);

            shoppingCart.setUpdateTime(new Date());
            shoppingCartMapper.upById(shoppingCart);
        }

        //4.查询获取返回对象

        ShoppingCartAddVo shoppingCartAddVo=shoppingCartMapper.selectById(id);



        return shoppingCartAddVo;
    }

    /**
     * 删除购物车中的商品
     *
     * @param shoppingCartSubDto
     */
    @Override
    public Boolean delShoppingCartSub(ShoppingCartSubDto shoppingCartSubDto) {
        String userId = TokenHolder.getCurrentId();
        String dishId = shoppingCartSubDto.getDishId();

        ShoppingCart shoppingCart = shoppingCartMapper.countByDishId(dishId,userId);

        Integer number = shoppingCart.getNumber();
        Boolean flag=null;

        if(number==1){
            String id = shoppingCart.getId();
            flag=  shoppingCartMapper.deleteById(id);


        }else {
            number-=1;
            shoppingCart.setNumber(number);
             flag = shoppingCartMapper.upById(shoppingCart);

        }



        return flag;
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Boolean delShoppingCart() {
        String userId = TokenHolder.getCurrentId();

        shoppingCartMapper.deleteByUserId(userId);

        return true;
    }


}
